<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>props的校验</h2>
      <child
        name="zhangsan"
        :age="20"
        :sex="1"
        :height="183"
        :weight="1200"
        size="small"
        phone=""
      ></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;
      const Child = {
        // props: ["name"],

        /*
          props可以接受的数据类型:
            String
            Number
            Boolean
            Array
            Object
            Date
            Function
            Symbol
        */

        props: {
          name: String,
          // 数组表示或
          age: [Number, String],
          // required: true表示必传参数
          sex: {
            type: Number,
            required: true,
          }, // 等价于 sex: Number
          height: {
            type: Number,
            default: 180,
          },
          // 自定义类型校验函数
          weight: {
            type: Number,
            validator(value) {
              // 通过返回true或者false来判断是否满足条件
              // if (value > 150) {
              //   return true;
              // } else {
              //   return false;
              // }
              return value > 150;
            },
          },
          size: {
            type: String,
            validator(val) {
              return ["large", "default", "small"].includes(val);
            },
          },
          phone: {
            type: String,
            validator(val) {
              const reg =
                /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/;
              return reg.test(val);
            },
          },
        },
        template: `
          <div>这是child子组件 - {{ name }} - 
          {{ age }} - {{ sex }} - {{ height }} - {{ weight }}
          </div>
        `,
      };

      const app = createApp({
        data() {
          return {};
        },
        components: {
          Child,
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
